<template>
  <div class="org-faultList">
    <!-- <el-scrollbar height="520px"> 420 -->
    <el-table
      :data="organStructure"
      style="width: 100%; margin-bottom: 10px"
      row-key="id"
      default-expand-all
      @cell-contextmenu="cellContextMenu"
      @cell-click="cellMouseLeave"
      @row-click="rowClick"
      class="rowData org-faultList-w"
      highlight-current-row
      :header-cell-style="{ background: '#e6f2f0', color: '#606266' }"
      :cell-class-name="tableCellClassName"
    >
      <el-table-column
        fixed
        prop="pname"
        width="200"
        label="组织结构"
        align="center"
      >
        <template #default="scope">
          <span v-if="scope.row.position">
            {{ scope.row.name }}
          </span>
          <span v-else>
            {{ scope.row.name ? scope.row.name : scope.row.pname }}
          </span>
        </template>
      </el-table-column>
      <el-table-column fixed width="80" label="表位" align="center">
        <template #default="scope">
          <template v-if="scope.row.position > 0">
            <el-input disabled v-model="scope.row.position"></el-input>
          </template>
        </template>
      </el-table-column>
      <el-table-column
        prop="meterModel"
        width="130"
        label="电表型号"
        align="center"
      >
        <template #default="scope">
          <p
            v-if="!scope.row.newMeterModel"
            @click="cellClick(scope.row, scope.column)"
          >
            {{ scope.row.meterModel }}
          </p>
          <el-select
            v-else
            v-model="scope.row.meterModel"
            class="m-2"
            style="width: 100px"
          >
            <el-option
              v-for="item in dbxh"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        prop="meterType"
        width="130"
        label="电表类型"
        align="center"
      >
        <template #default="scope">
          <p
            v-if="!scope.row.newMeterType"
            @click="cellClick(scope.row, scope.column)"
          >
            {{ scope.row.meterType }}
          </p>
          <el-select
            v-else
            v-model="scope.row.meterType"
            class="m-2"
            style="width: 100px"
          >
            <el-option
              v-for="item in sblx"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column
        prop="wiringType"
        width="130"
        label="接线方式"
        align="center"
      >
        <template #default="scope">
          <p
            v-if="!scope.row.newWiringType"
            @click="cellClick(scope.row, scope.column)"
          >
            {{ scope.row.wiringType }}
          </p>
          <el-select
            v-else
            v-model="scope.row.wiringType"
            class="m-2"
            style="width: 100px"
            @change="changeWiringType(scope.row.id)"
          >
            <template v-if="scope.row.type === 2">
              <el-option
                v-for="item in jxfs01"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
                {{ item.label }}
              </el-option>
            </template>
            <template v-else>
              <el-option
                v-for="item in jxfs"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
                {{ item.label }}
              </el-option>
            </template>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="voltage" width="140" label="电压" align="center">
        <template #default="scope">
          <p
            v-if="!scope.row.newVoltage"
            @click="cellClick(scope.row, scope.column)"
          >
            {{ scope.row.voltage }}
          </p>
          <el-select
            filterable
            v-else
            v-model="scope.row.voltage"
            class="m-2"
            style="width: 115px"
            @blur="selectBlur($event, scope.row, 'voltage')"
          >
            <el-option
              v-for="item in dy"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="current" width="130" label="电流" align="center">
        <template #default="scope">
          <p
            v-if="!scope.row.newCurrent"
            @click="cellClick(scope.row, scope.column)"
          >
            {{ scope.row.current }}
          </p>
          <el-select
            filterable
            v-else
            v-model="scope.row.current"
            class="m-2"
            style="width: 100px"
            @blur="selectBlur($event, scope.row, 'current')"
          >
            <el-option
              v-for="item in dl"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <!--      <el-table-column-->
      <!--        prop="userAddr"-->
      <!--        width="190"-->
      <!--        label="用户地址"-->
      <!--        align="center"-->
      <!--      >-->
      <!--        <template #default="scope">-->
      <!--          <template v-if="scope.row.type === 2 || scope.row.type === 6">-->
      <!--            <el-input-->
      <!--              disabled-->
      <!--              v-model="scope.row.userAddr"-->
      <!--              style="width: 160px"-->
      <!--            />-->
      <!--          </template>-->
      <!--          <template v-else>-->
      <!--            <el-input-->
      <!--              v-if="scope.row.type && [3, 4, 7, 8].includes(scope.row.type)"-->
      <!--              v-model="scope.row.userAddr"-->
      <!--              style="width: 160px"-->
      <!--            />-->
      <!--          </template>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column
        prop="meterAddr"
        width="190"
        label="通讯地址"
        align="center"
      >
        <template #default="scope">
          <template v-if="scope.row.type === 2">
            <el-input
              disabled
              v-model="scope.row.meterAddr"
              style="width: 160px"
            />
          </template>
          <template v-else>
            <el-input
              v-if="scope.row.meterAddr"
              v-model="scope.row.meterAddr"
              style="width: 160px"
            />
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="ptRatio" width="130" label="PT变化" align="center">
        <template #default="scope">
          <template
            v-if="
              scope.row.type === 2 ||
              scope.row.type === 6 ||
              scope.row.wiringType === '单相'
            "
          >
            <el-select
              v-if="scope.row.ptRatio"
              v-model="scope.row.ptRatio"
              class="m-2"
              style="width: 100px"
              disabled
            >
              <el-option label="---" value="" />
            </el-select>
          </template>
          <template v-else>
            <p
              v-if="!scope.row.newPtRatio"
              @click="cellClick(scope.row, scope.column)"
            >
              {{ scope.row.ptRatio }}
            </p>
            <el-select
              filterable
              v-else
              v-model="scope.row.ptRatio"
              class="m-2"
              style="width: 100px"
              @blur="selectBlur($event, scope.row, 'ptRatio')"
            >
              <el-option
                v-for="item in ptbh"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="ctRatio" width="130" label="CT变化" align="center">
        <template #default="scope">
          <template v-if="scope.row.type === 2">
            <el-select
              v-if="scope.row.ctRatio"
              v-model="scope.row.ctRatio"
              class="m-2"
              style="width: 100px"
              disabled
            >
              <el-option label="---" value="" />
            </el-select>
          </template>
          <template v-else>
            <p
              v-if="!scope.row.newCtRatio"
              @click="cellClick(scope.row, scope.column)"
            >
              {{ scope.row.ctRatio }}
            </p>
            <el-select
              filterable
              v-else
              v-model="scope.row.ctRatio"
              class="m-2"
              style="width: 100px"
              @blur="selectBlur($event, scope.row, 'ctRatio')"
            >
              <el-option
                v-for="item in ctbh"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </template>
        </template>
      </el-table-column>
      <el-table-column
        prop="installAddr"
        width="150"
        label="安装地址"
        align="center"
      >
        <template #default="scope">
          <p
            v-if="!scope.row.newInstallAddr"
            @click="cellClick(scope.row, scope.column)"
          >
            {{ scope.row.installAddr }}
          </p>
          <template v-else>
            <el-select
              v-if="scope.row.wiringType === '单相'"
              v-model="scope.row.installAddr"
              class="m-2"
              style="width: 100px"
            >
              <el-option
                v-for="item in azdz"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <!-- </el-scrollbar> -->
    <!--  系统设置 右键菜单 -->
    <div id="menu" class="menuDiv">
      <ul class="menuUl">
        <li
          v-for="(item, index) in menus"
          :key="index"
          @click.stop="infoClick(item.type)"
        >
          {{ item.name }}
        </li>
      </ul>
    </div>
    <!-- 右键菜单 -->
    <!--  总表 右键菜单 -->
    <RightClickMenu :tbMenus="tbMenus" @infoClickTb="infoClickTb">
    </RightClickMenu>
    <!-- 右键菜单 -->
    <!-- 添加用户表 -->
    <el-dialog
      v-model="addUserVisible"
      title="增加用户表"
      width="300"
      :before-close="handleClose"
    >
      <div>
        数量：<el-select
          v-model="bw"
          class="m-2"
          placeholder="Select"
          style="width: 100px"
        >
          <el-option
            v-for="item in bwList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleClose">退出</el-button>
          <el-button type="primary" @click="sure"> 确定 </el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 添加用户表 -->
    <!-- 台区下总表的用户表 -->
    <TablePosition
      :currentId="currentId"
      :currentPname="currentPname"
      @close="changeBwStatus1"
    ></TablePosition>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import TablePosition from "@/components/dialog/tablePosition.vue";
import RightClickMenu from "@/components/rightClickMenu.vue";
import { useAppStore, paramsDataStore } from "@/stores";
import { storeToRefs } from "pinia";
const props = defineProps({
  loading: Boolean,
});

const useApp = useAppStore();
let { tbPositionVisible, currentEpitope, tempData, organStructure } =
  storeToRefs(useApp);
const paramsData = paramsDataStore();
let { stationArea, newStationArea, selectedArea } = storeToRefs(paramsData);
const menus = ref([
  {
    id: 1,
    name: "增加台区",
    type: 1,
  },
  {
    id: 2,
    name: "增加专变",
    type: 2,
  },
  {
    id: 3,
    name: "增加用户表",
    type: 3,
  },
]);
const bwList = ref([
  {
    label: "1",
    value: "1",
  },
  {
    label: "2",
    value: "2",
  },
  {
    label: "3",
    value: "3",
  },
  {
    label: "4",
    value: "4",
  },
  {
    label: "5",
    value: "5",
  },
  {
    label: "6",
    value: "6",
  },
  {
    label: "7",
    value: "7",
  },
  {
    label: "8",
    value: "8",
  },
  {
    label: "9",
    value: "9",
  },
  {
    label: "10",
    value: "10",
  },
  {
    label: "11",
    value: "11",
  },
  {
    label: "12",
    value: "12",
  },
  {
    label: "13",
    value: "13",
  },
  {
    label: "14",
    value: "14",
  },
  {
    label: "15",
    value: "15",
  },
  {
    label: "16",
    value: "16",
  },
]);
// 电表类型
const dbxh = ref([
  {
    label: "DDS318",
    value: "DDS318",
  },
  {
    label: "TCC081E",
    value: "TCC081E",
  },
  {
    label: "DDS220-Z",
    value: "DDS220-Z",
  },
]);
// 电表型号
const sblx = ref([
  {
    label: "互感器表",
    value: "互感器表",
  },
  {
    label: "直通表",
    value: "直通表",
  },
]);
//接线方式
const jxfs = ref([
  {
    label: "三相四线",
    value: "三相四线",
  },
  {
    label: "三相三线",
    value: "三相三线",
  },
  {
    label: "单相",
    value: "单相",
  },
]);
const jxfs01 = ref([
  {
    label: "三相四线",
    value: "三相四线",
  },
  {
    label: "三相三线",
    value: "三相三线",
  },
]);
// 电压
const dy = ref([
  {
    label: "AC220/380V",
    value: "AC220/380V",
  },
  {
    label: "AC57.7/100V",
    value: "AC57.7/100V",
  },
]);
// 电流
const dl = ref([
  {
    label: "1.5(6)A",
    value: "1.5(6)A",
  },
  {
    label: "5(40)A",
    value: "5(40)A",
  },
  {
    label: "10(60)A",
    value: "10(60)A",
  },
]);
// 通讯地址
const txdz = ref([]);
//PT变化
const ptbh = ref([
  {
    label: "1V/1V",
    value: "1V/1V",
  },
  {
    label: "10kV/0.4kV",
    value: "10kV/0.4kV",
  },
]);
//CT变化
const ctbh = ref([
  {
    label: "50A/5A",
    value: "50A/5A",
  },
]);
//安装地址
const azdz = ref([
  {
    label: "A",
    value: "A",
  },
  {
    label: "B",
    value: "B",
  },
  {
    label: "C",
    value: "C",
  },
]);

const addUserVisible = ref(false);
const bw = ref("1");
const currentId = ref();
const currentPname = ref();
const currentData = ref([]);
const tbMenus = ref([
  {
    id: 4,
    name: "选择用户表",
    type: 4,
  },
  {
    id: 2,
    name: "删除",
    type: 2,
  },
]);

//点击事件
const cellClick = (row, column) => {
  switch (column.property) {
    case "meterModel":
      row.newMeterModel = true;
      console.log("meterModel", row);
      break;
    case "meterType":
      row.newMeterType = true;
      break;
    case "wiringType":
      row.newWiringType = true;
      break;
    case "voltage":
      row.newVoltage = true;
      break;
    case "current":
      row.newCurrent = true;
      break;
    case "ptRatio":
      row.newPtRatio = true;
      break;
    case "ctRatio":
      row.newCtRatio = true;
      break;
    case "installAddr":
      row.newInstallAddr = true;
      break;
    default:
      break;
  }
};
const tableCellClassName = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex === 0) {
    return "first-column-class";
  }
  return "";
};
//右键事件
const cellContextMenu = (row, column, cell, event) => {
  if (column.property === "pname" || column.property === "name") {
    currentId.value = row.id;
    if (row.pname && row.pname != undefined) currentPname.value = row.pname;
    currentData.value = row;
    if (row.pname === "系统设置") {
      let menu = document.querySelector("#menu");
      //阻止元素发生默认的行为
      event.preventDefault();
      // 根据事件对象中鼠标点击的位置，进行定位
      // console.log(event.clientX, event.clientY);
      menu.style.left = event.clientX - 135 + 10 + "px";
      menu.style.top = event.clientY - 320 - 30 + "px";
      // console.log(event.clientX, event.clientY);
      // 改变自定义菜单的隐藏与显示
      menu.style.display = "block";
      menu.style.zIndex = 1000;
    } else {
      if (row.name === "总表") {
        tbMenus.value = [
          {
            id: 4,
            name: "选择用户表",
            type: 4,
          },
          {
            id: 2,
            name: "删除",
            type: 2,
          },
        ];
      } else if (row.name === "采集终端" || row.name === "用户表") {
        tbMenus.value = [
          {
            id: 2,
            name: "删除",
            type: 2,
          },
        ];
      } else {
        let type = row.type;
        if (type === "1") {
          if (!row.children.length) {
            tbMenus.value = [
              {
                id: 7,
                name: "采集终端",
                type: 7,
              },
              {
                id: 6,
                name: "总表",
                type: 6,
              },
              {
                id: 2,
                name: "删除",
                type: 2,
              },
            ];
          } else if (row.children.length < 2) {
            row.children.forEach((item) => {
              if (item.name === "采集终端") {
                tbMenus.value = [
                  {
                    id: 6,
                    name: "增加总表",
                    type: 6,
                  },
                  {
                    id: 2,
                    name: "删除",
                    type: 2,
                  },
                ];
              }
              if (item.name === "总表") {
                tbMenus.value = [
                  {
                    id: 7,
                    name: "增加采集终端",
                    type: 7,
                  },
                  {
                    id: 2,
                    name: "删除",
                    type: 2,
                  },
                ];
              }
            });
          } else {
            tbMenus.value = [
              {
                id: 2,
                name: "删除",
                type: 2,
              },
            ];
          }
        } else if (type === "5") {
          if (!row.children.length) {
            tbMenus.value = [
              {
                id: 8,
                name: "增加采集终端",
                type: 8,
              },
              {
                id: 5,
                name: "增加用户表",
                type: 5,
              },
              {
                id: 2,
                name: "删除",
                type: 2,
              },
            ];
          } else if (row.children.length < 2) {
            row.children.forEach((item) => {
              if (item.name === "采集终端") {
                tbMenus.value = [
                  {
                    id: 5,
                    name: "增加用户表",
                    type: 5,
                  },
                  {
                    id: 2,
                    name: "删除",
                    type: 2,
                  },
                ];
              }
              if (item.name === "用户表") {
                tbMenus.value = [
                  {
                    id: 8,
                    name: "增加采集终端",
                    type: 8,
                  },
                  {
                    id: 2,
                    name: "删除",
                    type: 2,
                  },
                ];
              }
            });
          } else {
            tbMenus.value = [
              {
                id: 2,
                name: "删除",
                type: 2,
              },
            ];
          }
        } else {
          tbMenus.value = [
            {
              id: 2,
              name: "删除",
              type: 2,
            },
          ];
        }
      }
      let menu = document.querySelector("#tbMenu");
      //阻止元素发生默认的行为
      event.preventDefault();
      // 根据事件对象中鼠标点击的位置，进行定位
      menu.style.left = event.clientX - 115 + 10 + "px";
      menu.style.top = event.clientY - 315 - 30 + "px";
      // 改变自定义菜单的隐藏与显示
      menu.style.display = "block";
      menu.style.zIndex = 1000;
    }
  }
};
// 右键菜单点击事件
const infoClick = (index) => {
  //匹配当前表位
  let diffArr = extractBw(organStructure.value[0].children);
  organStructure.value.forEach((item, key) => {
    let child = [];
    if (currentEpitope.value >= 16) {
      ElMessage({
        message: "添加失败,超出表位数量",
        type: "warning",
      });
      return false;
    } else {
      let num = item.children.length + 1;
      let num1 = item.children.length + 2;
      let pname = "";
      switch (index) {
        case 1:
          pname = "台区" + num;
          child = {
            id: "4120" + diffArr[0],
            name: pname,
            type: 1,
            children: [
              {
                id: "4121" + diffArr[0],
                name: "采集终端",
                pname,
                type: 2,
                pid: 1,
                position: diffArr[0],
                meterModel: "DDS318",
                meterType: "互感器表",
                wiringType: "三相四线",
                voltage: "AC220/380V",
                current: "1.5(6)A",
                meterAddr: "",
                userAddr: "",
                ptRatio: "---",
                ctRatio: "---",
                installAddr: "",
              },
              {
                id: "4122" + diffArr[1],
                name: "总表",
                pname,
                type: 3,
                pid: 1,
                position: diffArr[1],
                meterModel: "DDS318",
                meterType: "互感器表",
                wiringType: "三相四线",
                voltage: "AC220/380V",
                current: "1.5(6)A",
                meterAddr: "",
                userAddr: "",
                ptRatio: "1V/1V",
                ctRatio: "50A/5A",
                installAddr: "",
              },
            ],
          };
          // item.children.push(child);
          useApp.setCurrentData(child);
          changeBwStatus1([diffArr[0], diffArr[1]], pname, [
            "采集终端",
            "总表",
          ]); //改变表位状态
          break;
        case 2:
          pname = "专变用户" + num;
          child = {
            id: "4123" + diffArr[0],
            name: pname,
            type: 5,
            children: [
              {
                id: "4124" + diffArr[0],
                name: "采集终端",
                pname,
                type: 6,
                position: diffArr[0],
                meterModel: "DDS318",
                meterType: "互感器表",
                wiringType: "三相四线",
                voltage: "AC220/380V",
                current: "1.5(6)A",
                meterAddr: "",
                userAddr: "",
                ptRatio: "---",
                ctRatio: "---",
                installAddr: "",
              },
              {
                id: "4125" + diffArr[1],
                name: "用户表",
                pname,
                type: 7,
                position: diffArr[1],
                meterModel: "DDS318",
                meterType: "互感器表",
                wiringType: "三相四线",
                voltage: "AC220/380V",
                current: "1.5(6)A",
                meterAddr: "",
                userAddr: "",
                ptRatio: "1V/1V",
                ctRatio: "50A/5A",
                installAddr: "",
              },
            ],
          };
          // item.children.push(child);
          useApp.setCurrentData(child);
          changeBwStatus1([diffArr[0], diffArr[1]], pname, [
            "采集终端",
            "用户表",
          ]); //改变表位状态
          break;
        case 3:
          addUserVisible.value = true;
          break;
        default:
          break;
      }
    }
  });
  let menu = document.querySelector("#menu");
  menu.style.display = "none";
};
const infoClickTb = (index) => {
  let pname = currentData.value.pname;
  let diffArr = extractBw(organStructure.value[0].children);
  if (index === 4) {
    tbPositionVisible.value = true;
  } else if (index === 2) {
    isDel();
  } else if (index === 5) {
    let child = {};
    organStructure.value[0].children.forEach((item) => {
      if (item.id === currentData.value.id) {
        child = {
          id: "4126" + diffArr[0],
          name: "用户表",
          pname,
          type: 7, //1台区；2台区下终端；3台区下总表；4台区下用户表；5转变用户；6专变下终端；7专变下用户表；8用户表
          pid: 0,
          position: diffArr[0],
          meterModel: "DDS318",
          meterType: "互感器表",
          wiringType: "三相四线",
          voltage: "AC220/380V",
          current: "1.5(6)A",
          meterAddr: "",
          userAddr: "",
          ptRatio: "1V/1V",
          ctRatio: "50A/5A",
          installAddr: "",
        };
        item.children.push(child);
      }
    });
    changeBwStatus1([diffArr[0]], pname, ["用户表"]); //改变表位状态
  } else if (index === 6) {
    let child = {};
    organStructure.value[0].children.forEach((item) => {
      if (item.id === currentData.value.id) {
        child = {
          id: "4127" + diffArr[0],
          name: "总表",
          pname,
          type: 3,
          pid: 0,
          position: diffArr[0],
          meterModel: "DDS318",
          meterType: "互感器表",
          wiringType: "三相四线",
          voltage: "AC220/380V",
          current: "1.5(6)A",
          meterAddr: "",
          userAddr: "",
          ptRatio: "1V/1V",
          ctRatio: "50A/5A",
          installAddr: "",
        };
        item.children.push(child);
      }
    });
    changeBwStatus1([diffArr[0]], pname, ["总表"]); //改变表位状态
  } else if (index === 7) {
    let child = {};
    organStructure.value[0].children.forEach((item) => {
      if (item.id === currentData.value.id) {
        child = {
          id: "4128" + diffArr[0],
          name: "采集终端",
          pname,
          type: 2,
          pid: 0,
          position: diffArr[0],
          meterModel: "DDS318",
          meterType: "互感器表",
          wiringType: "三相四线",
          voltage: "AC220/380V",
          current: "1.5(6)A",
          meterAddr: "",
          userAddr: "",
          ptRatio: "---",
          ctRatio: "---",
          installAddr: "",
        };
        item.children.push(child);
      }
    });
    changeBwStatus1([diffArr[0]], pname, ["采集终端"]); //改变表位状态
  } else if (index === 8) {
    let child = {};
    organStructure.value[0].children.forEach((item) => {
      if (item.id === currentData.value.id) {
        child = {
          id: "4129" + diffArr[0],
          name: "采集终端",
          pname,
          type: 6,
          position: diffArr[0],
          meterModel: "DDS318",
          meterType: "互感器表",
          wiringType: "三相四线",
          voltage: "AC220/380V",
          current: "1.5(6)A",
          meterAddr: "",
          userAddr: "",
          ptRatio: "---",
          ctRatio: "---",
          installAddr: "",
        };
        item.children.push(child);
      }
    });
    changeBwStatus1([diffArr[0]], pname, ["采集终端"]); //改变表位状态
  }
  let menu = document.querySelector("#tbMenu");
  menu.style.display = "none";
};
//匹配可用表位
const extractBw = (items) => {
  let arr = extractArr(items);
  let arrAll = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
  let newA = new Set(arrAll);
  let newB = new Set(arr);
  let diffArr = Array.from(new Set([...newA].filter((x) => !newB.has(x))));
  return diffArr;
};
const extractArr = (items, arr = []) => {
  items.forEach((item) => {
    if (item.position && item.position != undefined) arr.push(item.position);
    if (item.children) {
      extractArr(item.children, arr);
    }
  });
  // console.log(arr, items);
  return arr;
};
// 动态修改表位信息
const changeBwStatus1 = (arr = [], pname = "", nameArr = []) => {
  let index = 0;
  stationArea.value.forEach((item) => {
    if (arr.includes(Number(item.value))) {
      item.state = 1;
      item.checked = true;
      item.disabled = true;
      item.pname = pname;
      item.name = nameArr[index];
      selectedArea.value.push(Number(item.value));
      index++;
    }
  });
  newStationArea.value = JSON.parse(JSON.stringify(stationData.value));
};
const changeBwStatus0 = () => {
  let arr = [];
  if (currentData.value.name === "用户表") {
    arr = [currentData.value.position];
  } else {
    if (currentData.value.children != undefined && currentData.value.children) {
      arr = extractArr(currentData.value.children);
    } else {
      arr = [currentData.value.position];
    }
  }
  stationArea.value.forEach((item) => {
    if (arr.includes(Number(item.value)) || arr.includes(item.value)) {
      item.state = 0;
      item.checked = false;
      item.disabled = false;
      selectedArea.value = selectedArea.value.filter(
        (element) => element !== item.value
      );
    }
  });
  newStationArea.value = JSON.parse(JSON.stringify(stationData.value));
};
// 关闭增加用户弹窗
const handleClose = () => {
  addUserVisible.value = false;
};
//确定用户表数量
const sure = () => {
  let diffArr = extractBw(organStructure.value[0].children);
  let child = {};
  let pname = "用户表";
  for (let i = 1; i <= bw.value; i++) {
    child = {
      id: "4130" + diffArr[i - 1],
      name: pname,
      type: 8,
      pid: 0,
      position: diffArr[i - 1],
      meterModel: "DDS318",
      meterType: "互感器表",
      wiringType: "三相四线",
      voltage: "AC220/380V",
      current: "1.5(6)A",
      meterAddr: "",
      userAddr: "",
      ptRatio: "1V/1V",
      ctRatio: "50A/5A",
      installAddr: "",
    };
    useApp.setCurrentData(child);
    changeBwStatus1([diffArr[i - 1]], "", ["用户表"]); //改变表位状态
  }
  addUserVisible.value = false;
};
//是否删除
const isDel = () => {
  ElMessageBox.confirm("确认删除", "确认删除", {
    confirmButtonText: "确认",
    cancelButtonText: "放弃",
    type: "warning",
  })
    .then(() => {
      changeBwStatus0();
      useApp.delCurrentData(currentId.value);
      ElMessage({
        type: "success",
        message: "删除成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "warning",
        message: "删除失败",
      });
    });
};
//点击其他地方 关闭右键出来的菜单
const cellMouseLeave = () => {
  let menu = document.querySelector("#tbMenu");
  menu.style.display = "none";
  let menu1 = document.querySelector("#menu");
  menu1.style.display = "none";
};
//表格行点击事件
const rowClick = (row) => {
  if (row.pname != "系统设置") {
    tempData.value = row;
  }
};
//切换接线方式
const changeWiringType = (val) => {
  // console.log("changeWiringType", organStructure.value, val, event);
  sblx.value = [
    {
      label: "互感器表",
      value: "互感器表",
    },
    {
      label: "直通表",
      value: "直通表",
    },
  ];
  azdz.value = [];
  regData(organStructure.value[0].children, val);
};
const regData = (data, id) => {
  data.forEach((item) => {
    if (Number(id) === Number(item.id)) {
      if (item.wiringType === "三相四线") {
        dy.value = [
          {
            label: "AC220/380V",
            value: "AC220/380V",
          },
          {
            label: "AC57.7/100V",
            value: "AC57.7/100V",
          },
        ];
        item.voltage = "AC220/380V";
        item.meterType = "互感器表";
        item.ptRatio = "1V/1V";
        if (item.type === 2) item.ctRatio = "50A/5A";
        item.installAddr = "";
      } else if (item.wiringType === "三相三线") {
        dy.value = [
          {
            label: "AC100V",
            value: "AC100V",
          },
          {
            label: "AC380V",
            value: "AC380V",
          },
        ];
        item.voltage = "AC100V";
        item.meterType = "互感器表";
        item.ptRatio = "1V/1V";
        if (item.type === 2) item.ctRatio = "50A/5A";
        item.installAddr = "";
      } else {
        dy.value = [
          {
            label: "AC220V",
            value: "AC220V",
          },
        ];
        item.voltage = "AC220V";
        sblx.value = [
          {
            label: "直通表",
            value: "直通表",
          },
        ];
        azdz.value = [
          {
            label: "A",
            value: "A",
          },
          {
            label: "B",
            value: "B",
          },
          {
            label: "C",
            value: "C",
          },
        ];
        item.meterType = "直通表";
        item.ptRatio = "---";
        item.installAddr = "A";
        if (item.type === 2) item.ctRatio = "---";
      }
    }
    if (item.children) {
      regData(item.children, id);
    }
  });
};
const selectBlur = (event, obj, type) => {
  // console.log(event.target.value, obj.id, type);
  if (event.target.value.trim() !== "") {
    regDataCurrent(
      organStructure.value[0].children,
      obj.id,
      event.target.value,
      type
    );
  }
};
const regDataCurrent = (data, id, result, type) => {
  data.forEach((item) => {
    if (Number(id) === Number(item.id)) {
      switch (type) {
        case "voltage":
          item.voltage = result;
          break;
        case "current":
          item.current = result;
          break;
        case "ptRatio":
          item.ptRatio = result;
          break;
        case "ctRatio":
          item.ctRatio = result;
          break;
        default:
          break;
      }
    }
    if (item.children) {
      regDataCurrent(item.children, id, result, type);
    }
  });
};
</script>

<style lang="scss" scoped>
.org-faultList {
  position: relative;
  .menuDiv {
    display: none;
    position: absolute;
    .menuUl {
      height: auto;
      width: auto;
      font-size: 14px;
      text-align: left;
      border-radius: 3px;
      border: none;
      background-color: #c4c4c4;
      color: #fff;
      list-style: none;
      padding: 0 10px;

      li {
        width: 140px;
        height: 35px;
        line-height: 35px;
        cursor: pointer;
        border-bottom: 1px solid rgba(255, 255, 255, 0.47);

        &:hover {
          // background-color: rgb(26, 117, 158);
          color: rgb(54, 138, 175);
        }
      }
    }
  }
  .tbMenuDiv {
    display: none;
    position: absolute;
    .tbMenuUl {
      height: auto;
      width: auto;
      font-size: 14px;
      text-align: left;
      border-radius: 3px;
      border: none;
      background-color: #c4c4c4;
      color: #fff;
      list-style: none;
      padding: 0 10px;

      li {
        width: 140px;
        height: 35px;
        line-height: 35px;
        cursor: pointer;
        border-bottom: 1px solid rgba(255, 255, 255, 0.47);

        &:hover {
          // background-color: rgb(26, 117, 158);
          color: rgb(54, 138, 175);
        }
      }
    }
  }
}
</style>
<style lang="scss">
.org-faultList {
  .el-input__wrapper {
    padding: 1px 5px !important;
  }
}
.first-column-class {
  background-color: #b3eae4 !important;
  color: #606266;
}
</style>
